package com.youaishujuhui.devops.web.dao.game;

import java.util.List;

import javax.annotation.Resource;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;

import org.springframework.stereotype.Service;

import com.youaishujuhui.devops.web.domain.game.GameProcess;
import com.youaishujuhui.devops.web.po.HostPO;

@Service
public class GameProcessDao {
	
	@Resource(name = "nutDao")
    private Dao nutDao;
	
	public List<GameProcess> queryAll() {
		return nutDao.queryByJoin(GameProcess.class, null, Cnd.where("t_game_process.state", ">=", 0));
	}

	public List<HostPO> queryByGameIdAndChannelId(String gameId, String channelName) {
		Sql sql = Sqls.create("SELECT t_game_process.dir,t_game_process.serverId,t_host.ctccIp as host,t_host.sshPort as port\r\n" + 
				"FROM t_game_process,t_host\r\n" + 
				"WHERE t_game_process.hostId = t_host.hostId AND t_game_process.gameId = @gameId AND t_game_process.dir like @channelName \r\n" + 
				"AND t_game_process.state = 1 AND t_host.state = 1 " +
				"GROUP BY t_game_process.dir");
	    sql.params().set("gameId", gameId);
	    sql.params().set("channelName", "%\\_" + channelName + "\\_%");
	    	    
	    sql.setEntity(nutDao.getEntity(HostPO.class));
	    sql.setCallback(Sqls.callback.entities());
	    
	    nutDao.execute(sql);
	    return sql.getList(HostPO.class);
	}
	
}
